#!/bin/zsh

echo "󰯁 Setup isync"
mkdir -p {{ joinPath .chezmoi.homeDir .path.mail }}/qq
mbsync -a
mu init --maildir={{ joinPath .chezmoi.homeDir .path.mail }}
mu index
brew services stop isync
brew services start isync --file=/dev/stdin <<EOF
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
        <key>EnvironmentVariables</key>
        <dict>
                <key>PATH</key>
                <string>{{ .path.brew }}/bin:{{ .path.brew }}/sbin:/usr/bin:/bin:/usr/sbin:/sbin</string>
                <key>PASSWORD_STORE_DIR</key>
                <string>{{ joinPath .chezmoi.homeDir .path.pass }}</string>
        </dict>
        <key>Label</key>
        <string>homebrew.mxcl.isync</string>
        <key>LimitLoadToSessionType</key>
        <array>
                <string>Aqua</string>
                <string>Background</string>
                <string>LoginWindow</string>
                <string>StandardIO</string>
                <string>System</string>
        </array>
        <key>ProgramArguments</key>
        <array>
                <string>{{ .path.brew }}/bin/mbsync</string>
                <string>-a</string>
        </array>
        <key>RunAtLoad</key>
        <true/>
        <key>StandardErrorPath</key>
        <string>{{ .chezmoi.homeDir }}/Library/Logs/isync/isync-stderr.log</string>
        <key>StandardOutPath</key>
        <string>{{ .chezmoi.homeDir }}/Library/Logs/isync/isync-stdout.log</string>
        <key>StartInterval</key>
        <integer>300</integer>
</dict>
</plist>
EOF
